package com.zlp.mall.modules.pms.controller;


import com.zlp.mall.common.api.Pager;
import com.zlp.mall.common.api.R;
import com.zlp.mall.modules.pms.dto.req.BrandAddReq;
import com.zlp.mall.modules.pms.dto.resp.BrandResp;
import com.zlp.mall.modules.pms.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author ZouLiPing
 * @since 2021-09-23
 */
@RestController
@RequestMapping("/brand")
@RequiredArgsConstructor
@Api(value = "brand", tags = "后台品牌模块")
public class BrandController {

    private final BrandService brandService;

    @GetMapping(value = "/list")
    @ApiOperation(value = "获取品牌分页列表")
    public R<Pager<BrandResp>> list(
            @RequestParam(value="keyword",defaultValue = "") String keyword,
            @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value="pageSize",defaultValue = "5") Integer pageSize)
    {

        Pager<BrandResp> page = brandService.list(keyword, pageNum, pageSize);
        return R.success(page);
    }

    @PostMapping(value = "/update/showStatus")
    @ApiOperation(value = "修改是否显示")
    public R updateShowStatus(
            @RequestParam(value = "ids") @ApiParam(value = "ids", name = "ids") List<Long> ids,
            @RequestParam(value = "showStatus") @ApiParam(value = "showStatus", name = "显示状态：0->不显示；1->显示") Integer showStatus
    ) {

        boolean result = brandService.updateShowStatus(ids, showStatus);
        return R.success(result);
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据ID获取品牌信息")
    public R<BrandResp> getById(@PathVariable Long id) {

        BrandResp brandResp = brandService.getInfoById(id);
        return R.success(brandResp);
    }

    @GetMapping(value = "delete/{id}")
    @ApiOperation(value = "根据ID删除品牌")
    public R delete(@PathVariable Long id) {

        boolean result = brandService.delete(id);
        return R.success(result);
    }

    @ApiOperation(value = "添加品牌")
    @PostMapping(value = "/create")
    public R create(@Validated @RequestBody BrandAddReq brandAddReq) {

        boolean result = brandService.createBrand(brandAddReq);
        if (result) {
            return R.success(result);
        }
        return R.failed();
    }

    @ApiOperation(value = "更新品牌")
    @PostMapping(value = "/update/{id}")
    public R update(@PathVariable("id") Long id,
                    @Valid @RequestBody BrandAddReq brandAddReq) {

        boolean result = brandService.updateBrand(id,brandAddReq);
        if (result) {
            return R.success(result);
        }
        return R.failed();
    }

}

